/*
 * sched.h - Estructures i macros pel tractament de processos
 */

#ifndef __SCHED_H__
#define __SCHED_H__

#include <list.h>
#include <mm_address.h>
#include <errno.h>
#include <filesystem.h>

#define NR_TASKS 10
#define NR_SEM 10
#define CHTABLE_SIZE 10
#define KERNEL_STACK_SIZE 1024
#define DEF_QUANTUM 10
#define NUM_PAG_DATA 20


/* Variables */

int pid;
int is_eoi;
int quantum;struct list_head runqueue;
struct list_head keyboardqueue;

/* Structs */

struct semaphore
{
	int n_sem;
	int owner;
	int sleepers;
	unsigned int value;
	struct list_head sem_list;
	struct list_head waitqueue;
} sems[NR_SEM];

struct channel
{
	char used;
	struct oft_entry* opened_file;
};

struct task_struct
{
	int pid;
	int quantum;
	int tics;
	int total_tics;
	int total_trans;
	char * kdb_buffer;
	int kdb_chars;
	struct list_head run_list;
	struct list_head sem_list;
	int data_frames[NUM_PAG_DATA];
	struct channel chtable[CHTABLE_SIZE];
};

union task_union
{
	struct task_struct task;
	unsigned long stack[KERNEL_STACK_SIZE];    /* pila de sistema, per procés */
};

struct protected_task_struct
{
	/* This field protects the different task_structs, so any acces to this field will generate a PAGE FAULT exeption */
	unsigned long task_protect[KERNEL_STACK_SIZE];
	union task_union t;
};

extern struct protected_task_struct task[NR_TASKS];

/* Functions */

struct task_struct * list_head_to_task_struct(struct list_head * l);
struct semaphore* list_head_to_sem(struct list_head *l);

struct task_struct * current();
void init_task0(void);	/* Inicialitza les dades del proces inicial */
int alloc_task();
int alloc_sem(int n_sem);
int search_pid(int pid);
void task_switch(union task_union *t);
void scheduling();

#endif  /* __SCHED_H__ */
